import Vue from "vue";
// import VueRouter from "vue-router";
import Router from 'vue-router'
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error=> error)
}
Vue.use(Router);

const router = new Router({
    mode:'hash',
    routes:[
        {
            path:'/login',
            name:'login',
            component:()=>import('@/views/LoGin.vue')
        },
        {
            path:'/home',
            name:'home',
            // redirect:'/welcome',
            component:()=>import('@/views/Home.vue'),
            children:[
                {
                    path:'/welcome',
                    component:()=>import('@/pages/home/welcome/Welcome.vue')
                },
                {
                    path:'/users',//  /home/users
                    name:'users',
                    component:()=>import('@/pages/home/users/Users.vue')
                },
                {
                    path:'/roles',
                    name:'roles',
                    component:()=>import('@/pages/home/rolesmanage/RolesList.vue')
                },
                {
                    path:'/rights',
                    name:'rights',
                    component:()=>import('@/pages/home/rolesmanage/RightsList.vue')
                },
                {
                    path:'/goods',
                    name:'goods',
                    component:()=>import('@/pages/home/goods/Goods.vue')
                },
                {
                    path:'/params',
                    name:'params',
                    component:()=>import('@/pages/home/goods/Params.vue')
                },
                {
                    path:'/categories',
                    name:'categories',
                    component:()=>import('@/pages/home/goods/Categories.vue')
                },
                {
                    path:'/orders',
                    name:'orders',
                    component:()=>import('@/pages/home/order/Orders.vue')
                },
                {
                    path:'/reports',
                    name:'reports',
                    component:()=>import('@/pages/home/reports/Reports.vue')
                },
                {
                    path:'/',
                    redirect:'/welcome'
                },
            ]
        },
        {
            path:'/mockdemo',
            name:'mockdemo',
            component:()=>import('@/pages/mockDemo/MockDemo.vue')
        },
        {
            path:'/',
            redirect:'/login'
        }
    ]
})

router.beforeEach((to,from,next)=>{
    console.log(to);
    console.log(from);
    if(to.path == '/login'){
        return next();
    }
    if(!window.sessionStorage.getItem('token')){
        return next('/login');
    }
    next();



})
export default router;